﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleTaskManager.Domain.Entities;
using SimpleTaskManager.Domain.Abstract;
using System.Data;

namespace SimpleTaskManager.Domain.Concrete
{
    /// <summary>
    /// A repository containing the entries of the working hours, and interaction logic on them.
    /// </summary>
    public class EFWorkingHoursEntryRepository : IWorkingHoursEntryRepository
    {
        private EFDbContext context = new EFDbContext();

        /// <summary>
        /// Gets the collection of the entries
        /// </summary>
        public IQueryable<WorkingHoursEntry> WorkingHoursEntries
        {
            get { return context.WorkingHoursEntries; }
        }

        /// <summary>
        /// Adds a new entry to the collection
        /// </summary>
        /// <param name="myHours">The entry to add</param>
        public void Add(WorkingHoursEntry myHours)
        {
            context.WorkingHoursEntries.Add(myHours);
            context.SaveChanges();
        }

        /// <summary>
        /// Removes an entry from the collection
        /// </summary>
        /// <param name="id">The ID of the entry to remove</param>
        public void Delete(int id)
        {
            WorkingHoursEntry hours = context.WorkingHoursEntries.Single(h => h.WorkingHoursEntryID == id);
            context.WorkingHoursEntries.Remove(hours);
            context.SaveChanges();
        }

        /// <summary>
        /// Saves the modifications of an entry
        /// </summary>
        /// <param name="hours">The enty to modify</param>
        public void Edit(WorkingHoursEntry hours)
        {
            context.Entry(hours).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}
